<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class DetailController extends Controller
{
    //
    public function detail(Request $request,$id){
        // dd($id);
        // 获取商品信息，库存信息
        $data = DB::table('t_goods as g')
            -> join('t_stock as s','g.gid','=','s.gid')
            -> select('g.*','s.count','s.total') 
            -> where('g.gid',$id)
            -> first();
        // 获取评论信息
        $comment_num = DB::table('t_comment as c')
            -> where('c.gid','$id')
            -> count('c.gid');
        // 获取评价等级
        $level_avg = DB::table('t_comment as c')
            -> where('c.gid',$id)
            -> avg('level');
        // 获取小图
        $smallPic = DB::table('t_photo')
            -> where('gid',$id)
            -> where('small','1')
            -> limit(3)
            -> get();
            // dd($smallPic);
        // 获取详情图
        $detailPic = DB::table('t_photo')
            -> where('gid',$id)
            -> where('small','0')
            ->get();
            // dd($data);
            // 获取评论内容
        $comment = DB::table('t_comment as c')
            -> join('users','users.id','=','c.id')
            -> where('c.gid',$id)
            -> get();
            // dd($comment);
        return view('home.detail.index'
            ,['data' => $data,'comment_num' => $comment_num,'level_avg' =>round($level_avg,2),'smallPic'=>$smallPic,'detailPic'=>$detailPic,'comment_content'=>$comment,'total'=>count($comment)]);
        // 最后的2是控制返回的小数位数
    }
}
